package com.mia.craftstudio.minecraft.forge;

import com.mia.craftstudio.CSProject;
import com.mia.craftstudio.CraftStudioLib;
import com.mia.craftstudio.IPackReaderCallback;
import com.mia.craftstudio.api.ICSProject;
import com.mia.craftstudio.minecraft.AnimationState;
import com.mia.craftstudio.minecraft.BlockDimensionalPosition;
import com.mia.craftstudio.minecraft.CSMsgAnimationUpdate;
import com.mia.craftstudio.minecraft.IAnimatedTile;
import java.io.InputStream;
import java.util.Locale;

import cpw.mods.fml.common.*;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.config.Configuration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(
   modid = "ptrmodellib",
   name = "ptrmodellib",
   version = "1.0.5",
   acceptedMinecraftVersions = "[1.7.10]"
)
public class CSLibMod {
   @Mod.Instance("ptrmodellib")
   public static CSLibMod instance;
   @SidedProxy(
      clientSide = "com.mia.craftstudio.minecraft.forge.ProxyClient",
      serverSide = "com.mia.craftstudio.minecraft.forge.ProxyCommon"
   )
   public static ProxyCommon proxy;
   public static final String modid = "ptrmodellib";
   public static Logger log = LogManager.getLogger("ptrmodellib");
   public static boolean displayOutline = false;
   private static Configuration config = null;
   private SimpleNetworkWrapper network;

   @Mod.EventHandler
   public void preInit(FMLPreInitializationEvent ev) {
      this.network = NetworkRegistry.INSTANCE.newSimpleChannel("CraftStudioLib");
      this.network.registerMessage((Class)CSMsgAnimationUpdate.Handler.class, CSMsgAnimationUpdate.class, 0, Side.CLIENT);
      config = new Configuration(ev.getSuggestedConfigurationFile());

      try {
         config.load();
         displayOutline = config.getBoolean("displayOutline", "general", false, "Should we render the targeting outline ? Set false for better performances.");
      } catch (Exception var6) {
         FMLLog.severe("PTRModelLib has a problem loading it's configuration", new Object[0]);
         FMLLog.severe(var6.getMessage(), new Object[0]);
         throw new RuntimeException(var6);
      } finally {
         if (config.hasChanged()) {
            config.save();
         }

      }

      proxy.preInit();
   }

   public static void startAnimation(TileEntity tile, ICSProject project, Integer animationID) {
      if (tile instanceof IAnimatedTile) {
         BlockDimensionalPosition tilePosition = ((IAnimatedTile)tile).getBlockPosDim();
         AnimationState anim = new AnimationState(project, animationID, System.currentTimeMillis(), 5, false);
         instance.network.sendToAllAround(new CSMsgAnimationUpdate((IAnimatedTile)tile, anim), new NetworkRegistry.TargetPoint(tilePosition.getDim(), (double)tilePosition.getX(), (double)tilePosition.getY(), (double)tilePosition.getZ(), 128.0D));
      }

   }

   public static ICSProject getCSProject(String name) {
      ModContainer mod = Loader.instance().activeModContainer();
      return new CSProject(name, mod.getModId(), String.format("/assets/%s/models/%s", mod.getModId().toLowerCase(Locale.US), name), mod.getSource());
   }

   public static ICSProject getCSProjectAndLoad(String name, InputStream overrideStream, IPackReaderCallback callback) {
      CraftStudioLib.getTimer().start("Full project");
      ICSProject project = getCSProject(name).addAllPacks(callback).loadPacks(overrideStream);
      CraftStudioLib.getTimer().stop("Full project");
      CraftStudioLib.debug(String.format("Project loaded in %s ms", CraftStudioLib.getTimer().get("Full project")));
      return project;
   }
}
